package com.bootdo.system.service.impl;

import com.bootdo.common.utils.ConversionUtil;
import com.bootdo.common.utils.R;
import com.bootdo.system.dao.DlCompanyDao;
import com.bootdo.system.domain.DlCompanyDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.bootdo.system.dao.DlJobDao;
import com.bootdo.system.domain.DlJobDO;
import com.bootdo.system.service.DlJobService;



@Service
public class DlJobServiceImpl implements DlJobService {
	@Autowired
	private DlJobDao dlJobDao;
	@Autowired
	private DlCompanyDao dlCompanyDao;
	@Autowired
	private RedisTemplate redisTemplate;
	@Value("${vx.appId}")
	private String appId;
	@Value("${vx.appSecret}")
	private String appSecret;
	@Override
	public DlJobDO get(Integer jobId){
		return dlJobDao.get(jobId);
	}
	
	@Override
	public List<DlJobDO> list(Map<String, Object> map){
		List<DlJobDO> list = dlJobDao.list(map);
		//获取公司名称，填入职位
		for (DlJobDO dlJobDO : list) {
			DlCompanyDO dlCompanyDO = dlCompanyDao.get(dlJobDO.getCompanyId());
			dlJobDO.setCompanyName(dlCompanyDO.getFullName());
		}
		return list;
	}
	
	@Override
	public int count(Map<String, Object> map){
		return dlJobDao.count(map);
	}
	
	@Override
	public int save(DlJobDO dlJob){
		return dlJobDao.save(dlJob);
	}
	
	@Override
	public int update(DlJobDO dlJob){
		if("1".equals(dlJob.getAuditStatus())){
			String accessToken = ConversionUtil.getAccessToken(appId, appSecret);
			String qrCode = ConversionUtil.getQRCode(accessToken, dlJob.getJobId().toString());
			dlJob.setQrcodePhoto(qrCode);
		}
		return dlJobDao.update(dlJob);
	}
	
	@Override
	public int remove(Integer jobId){
		return dlJobDao.remove(jobId);
	}
	
	@Override
	public int batchRemove(Integer[] jobIds){
		return dlJobDao.batchRemove(jobIds);
	}

    @Override
    public R auditFailureText(String jobId, String auditFailureText) {
		redisTemplate.opsForValue().set(jobId + "jobAuditFailureTex", auditFailureText);
		return R.ok();
    }

}
